package com.colorfruit.framework.common.util;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;

/**
 * 写错名字了,MD5
 */
@Slf4j
public class Md5Util {
    /**
     * md5 32位 小写 加密
     * @param str
     * @return
     */
    public static String encryption(String str) {
        log.info("md5的串 = {}",str);
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update((str).getBytes("UTF-8"));
            byte b[] = md5.digest();

            int i;
            StringBuffer buf = new StringBuffer("");

            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            log.info("md5转换后的串 = {}",buf.toString());
            return buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String encryption = encryption("13530471313");
        System.out.println(encryption);
    }
}
